package exportacao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class LerXML {
	
	
	@SuppressWarnings({ "finally", "static-access" })
	public List<BeanLerXML> listaValores(String file, String no) {
				
		List<BeanLerXML> lista = new ArrayList<BeanLerXML>();
		
		try {
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();

			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

			File fXmlFile = new File(file);

			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();

			Element elemento = (Element) doc.getDocumentElement();

			NodeList smp = ((Element) elemento.getElementsByTagName(no)
					.item(0)).getElementsByTagName("Unidade");

			for (int i = 0; i < smp.getLength(); i++) {
				BeanLerXML beanLerXML = new BeanLerXML();
				Node n = smp.item(i);
				if (n.getNodeType() == n.ELEMENT_NODE) {
					Element eElement = (Element) n;										
					beanLerXML.setUnidadePrimaria(eElement.getAttribute("Primaria").toString().equalsIgnoreCase("")?"0":eElement.getAttribute("Primaria").toString());
					verificaPeriodo(eElement, beanLerXML, lista);					
				}
			}					
						
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			return lista.isEmpty()? null:lista;
		}
	}
	
	/**
	 * Metodo que verifica o periodo do XML.
	 * @param eElement
	 * 			passa o elemento unidade primaria como parametro.
	 */
	public void verificaPeriodo(Element eElement, BeanLerXML beanLerXML, List<BeanLerXML> lista) {

		NodeList per = eElement.getElementsByTagName("Periodo");
		List<String> periodos = new ArrayList<String>();				
		for (int m = 0; m < per.getLength(); m++) {
			Node p = per.item(m);
			Element ePer = (Element) p;			
			periodos.add(ePer.getAttribute("Coleta").toString().equalsIgnoreCase("")?"0":ePer.getAttribute("Coleta").toString());			
		}		
		beanLerXML.setPeriodos(periodos);		
		verificaFatorPonderacaoEValores(eElement, beanLerXML, lista);
	}
	
	/**
	 * Metodo que verifica os valores do fator de ponderacao.
	 * @param eElement
	 * 			passa o elemento do codigo de unidade primaria como parametro.
	 * @param eElement
	 * 			passa o elemento do periodo como parametro
	 */
	public void verificaFatorPonderacaoEValores(Element eElement, BeanLerXML beanLerXML, List<BeanLerXML> lista){
		List<String> ponderacao = new ArrayList<String>();
		List<String> valores = new ArrayList<String>();
		
		NodeList fat = eElement.getElementsByTagName("Fator");
		for (int i = 0; i < fat.getLength(); i++) {
			Node f = fat.item(i);
			Element eFat = (Element) f;
			ponderacao.add(eFat.getAttribute("valor").toString().equalsIgnoreCase("")?"0":eFat.getAttribute("valor").toString());
		}
		beanLerXML.setPonderacao(ponderacao);
		
		NodeList cont = eElement.getElementsByTagName("Conteudo");
		for (int l = 0; l < cont.getLength(); l++) {
			Node c = cont.item(l);
			Element eCont = (Element) c;
			valores.add(eCont.getAttribute("valor").toString().equalsIgnoreCase("")?"0":eCont.getAttribute("valor").toString());
		}
		beanLerXML.setValores(valores);
		
		lista.add(beanLerXML);
	}
	
	public List<BeanLerXML> testaComparacaoXMLVisent(List<BeanLerXML> listaCliente, List<BeanLerXML> listaVisent){
		List<BeanLerXML> listaErros = new ArrayList<BeanLerXML>();
		for (BeanLerXML beanVisent : listaVisent) {
			int index = 0;
			for (BeanLerXML beanCliente : listaCliente) {
				if(Integer.parseInt(beanVisent.getUnidadePrimaria()) !=
						Integer.parseInt(beanCliente.getUnidadePrimaria())){
					index++;
				}else{
					boolean flag = false;
					for (int i = 0; i < beanVisent.getPonderacao().size(); i++) {
						if(Integer.parseInt(beanVisent.getPonderacao().get(i)) !=
								Integer.parseInt(beanCliente.getPonderacao().get(i))){
							flag = true;
						}
					}
					if(flag){
						index++;
					}else{
						boolean flag2 = false;
						for (int i = 0; i < beanVisent.getValores().size(); i++) {
							if(Integer.parseInt(beanVisent.getValores().get(i)) != 
									Integer.parseInt(beanCliente.getValores().get(i))){
								flag2 = true;
							}
						}
						if(flag2){
							index++;
						}
					}
				}
			}	
			if(index == listaCliente.size()){
				listaErros.add(beanVisent);				
			}
		}
		return listaErros;
	}
	
	public List<BeanLerXML> testaComparacaoXMLCliente(List<BeanLerXML> listaCliente, List<BeanLerXML> listaVisent){
		List<BeanLerXML> listaErros = new ArrayList<BeanLerXML>();
		for (BeanLerXML beanCliente : listaCliente) {
			int index = 0;
			for (BeanLerXML beanVisent : listaVisent) {
				if(Integer.parseInt(beanVisent.getUnidadePrimaria()) !=
						Integer.parseInt(beanCliente.getUnidadePrimaria())){
					index++;
				}else{
					boolean flag = false;
					for (int i = 0; i < beanVisent.getPonderacao().size(); i++) {
						if(Integer.parseInt(beanVisent.getPonderacao().get(i)) !=
								Integer.parseInt(beanCliente.getPonderacao().get(i))){
							flag = true;
						}
					}
					if(flag){
						index++;
					}else{
						boolean flag2 = false;
						for (int i = 0; i < beanVisent.getValores().size(); i++) {
							if(Integer.parseInt(beanVisent.getValores().get(i)) != 
									Integer.parseInt(beanCliente.getValores().get(i))){
								flag2 = true;
							}
						}
						if(flag2){
							index++;
						}
					}
				}
			}	
			if(index == listaVisent.size()){
				listaErros.add(beanCliente);				
			}
		}
		return listaErros;
	}
	
}
